package com.common.util;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

public class JpaPageable {
	private int page = 1;
	private int limit = 5;
	private String sort = "id";
	private String dir = "asc";

	public JpaPageable() {
		super();
	}

	public JpaPageable(int page) {
		super();
		this.page = page;
		this.limit = 5;
		this.sort = "id";
		this.dir = "desc";
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public String getSort() {
		return sort;
	}

	public int getPage() {
		return page;
	}

	public int getLimit() {
		return limit;
	}

	public String getDir() {
		return dir;
	}

	public Pageable getPageable() {
		Sort sortable = new Sort(Direction.DESC, sort);
		if (this.dir.equals("asc")) {
			sortable = new Sort(Direction.ASC, sort);
		}
		Pageable pageable = new PageRequest(this.page - 1, this.limit, sortable);
		return pageable;
	}
}
